import os
import logging
from functools import reduce
from sys import argv

from parsel import Selector
from commens import rmHTML, reTitle, dump_to_json



def str_con(slist):
    if len(slist)==0:
        return None
    return reduce(lambda x,y: x+y,list(filter(lambda x:x!='',map(lambda x:x.strip(' \t\n'),slist))))


def map_table(trs):
    content = []
    trs = trs[1:]
    for tr in trs:
        argm = {
            # "参数标识": tr.xpath('td//text()')[0].get(),
            "参数标识": rmHTML.sub('', tr.xpath('td')[0].get()).strip('\n\t '),
            "参数名称": rmHTML.sub('', tr.xpath('td')[1].get()).strip('\n\t '),
            "参数说明": rmHTML.sub('', tr.xpath('td')[2].get()).strip('\n\t ')+'\n'
        }
        content.append(argm)
    return content


def parse_file(f):

    oneMML = {}
    html = Selector(f.read())
    MML = html.xpath('/html/body/div[1]/h1/text()').get()
    logMAPPING.debug(MML)
    try:
        MML = reTitle.search(MML)
        if not MML:
            return None, None
        MML = MML[1]
        sections = html.xpath('/html/body/div[1]/div/div')
        logMAPPING.debug("有%d个section" % len(sections))
        for s in sections:
            title = s.css('.sectiontitle2contents::text').get()
            if title == "注意事项":
                lis = s.xpath('ul').get()
                if lis is not None:
                    content = rmHTML.sub('', lis)
                else:
                    content = s.xpath('descendant::p//text()').get()
            elif title == "参数说明":
                trs = s.xpath('div[1]//table//tr')
                if trs is not None:
                    content = map_table(trs)
                else:
                    content = None
            else:
                content = str_con(s.xpath('descendant::p//text()').getall())
            if title is None:
                continue
            oneMML[title] = content
            logMAPPING.debug(title + "\t" + str(content))
        return MML, oneMML
    except Exception as error:
        print(f'ERROR: {MML} {error}')
        logMAPPING.error(f'ERROR: {MML} {error}')
        return None,None


def list_file(dir_path='.', suffix='.py'):
    return [x for x in os.listdir(dir_path) if os.path.splitext(x)[1] == suffix]


def map_file(pathBase,fileList):
    mmlDict = {}
    for i in range(0, len(fileList)):
        logMAPPING.debug('第%d个文件' % i)
        file = os.path.join(pathBase, fileList[i])
        with open(file, 'r') as f:
            k, v = parse_file(f)
            if k is None:
                # print("无用文件：",fileList[i])
                continue
            elif k in mmlDict.keys():
                print("重复：",fileList[i])
                continue
            mmlDict[k] = v
            logMAPPING.debug(k,v)
            logMAPPING.debug('------------------------------------------------------------------------')
        # if i == 100:
        #     break
    return mmlDict


def main():
    suffix = '.html'
    pathNow = os.path.abspath('.')
    print("当前目录：", pathNow)
    if len(argv) < 2:
        pathBase = input("请输入文件夹地址(若是当前目录则直接回车)：")
    else:
        pathBase = argv[1]
    if pathBase == "":
        pathBase = pathNow
    print(pathBase)
    fileList = list_file(pathBase, suffix)
    fileListSize = len(fileList)
    print("共找到%d个文件" % fileListSize)
    for i in range(0, 9 if 9 < fileListSize else fileListSize):
        print(fileList[i])
    print("...")
    MMLmap = map_file(pathBase,fileList)
    dump_to_json(MMLmap)
    print('%d条数据已经转换为json'%len(MMLmap))


if __name__ == '__main__':
    logMAPPING = logging.getLogger("MAPPING")
    logMAPPING.setLevel(logging.DEBUG)
    hdMAPPING = logging.FileHandler("MAPPING.log", mode='w', encoding='utf-8')
    hdMAPPING.setLevel(logging.DEBUG)
    logMAPPING.addHandler(hdMAPPING)
    main()


